﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data.SqlServerCe;

namespace ProjectoPV_ESW.Account.Relatorio
{
    /// <summary>
    /// Classe desenvolvida por Mário Pereira
    /// </summary>
    public partial class VerRelatorio2 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["IDAlunoEditarRelatorio"] == null)
                Response.Redirect("EditarRelatorio1.aspx");
            else
            {
                CriancaId = System.Convert.ToInt32(Session["IDAlunoEditarRelatorio"]);
            }
        }
        public int CriancaId { get; set; }

        protected void ButtonVoltar_Click(object sender, EventArgs e)
        {
            Response.Redirect("EditarRelatorio1.aspx");
        }

        public int Dia { get; set; }
        public int Mes { get; set; }
        public int Ano { get; set; }

        protected void Calendar1_SelectionChanged(object sender, EventArgs e)
        {
            TextBox1.Text = "";
            DateTime dataNova = Calendar1.SelectedDate;
            Dia = dataNova.Day;
            Mes = dataNova.Month;
            Ano = dataNova.Year;


            string connectionString = ConfigurationManager.ConnectionStrings["JardimEntitiesString"].ConnectionString;
            SqlCeConnection conn = new SqlCeConnection(connectionString);

            string command="SELECT *";
            command+="FROM[RelatorioEducando]";
            command+="WHERE(dia = @dia AND mes = @mes AND ano = @ano AND Crianca_CriancaId = @crianca)";
            SqlCeCommand selectCommand = new SqlCeCommand(command, conn);
            selectCommand.Parameters.AddWithValue("@dia", Dia);
            selectCommand.Parameters.AddWithValue("@mes", Mes);
            selectCommand.Parameters.AddWithValue("@ano", Ano);
            selectCommand.Parameters.AddWithValue("@crianca",CriancaId);
            SqlCeDataReader reader;
            try
                {
                    conn.Open();
                    reader = selectCommand.ExecuteReader();
                    while (reader.Read())
                    {
                        this.TextBox1.Text = (string)reader["Conteudo"];
                    }
                    if (this.TextBox1.Text == "")
                    {
                        this.LabelErro.Text = "Não existe relatório neste dia!";
                        this.LabelOK.Text = "";
                        this.TextBox1.ReadOnly = true;
                        this.ButtonEditar.Visible = false;
                    }else
                    {
                        this.LabelErro.Text = "";
                        this.LabelOK.Text = "";
                        this.TextBox1.ReadOnly = false;
                        this.ButtonEditar.Visible = true;
                    }
                }
                catch (Exception exc)
                {
                    LabelErro.Text = exc.Message + "-" + exc.Source;
                }
                finally
                {
                    conn.Close();
                }
        }

        protected void ButtonEditar_Click(object sender, EventArgs e)
        {
            string connectionString = ConfigurationManager.ConnectionStrings["JardimEntitiesString"].ConnectionString;
            SqlCeConnection conn = new SqlCeConnection(connectionString);
            string conteudo = TextBox1.Text;

            string updt= "UPDATE [RelatorioEducando] SET [Conteudo] = @conteudo WHERE [dia]= @dia AND [mes] = @mes  AND [ano] = @ano  AND [Crianca_CriancaId] = @crianca";


            //LabelErro.Text = "Conteudo: " + conteudo + " Dia: " + Calendar1.SelectedDate.Day + " Mes: " + Calendar1.SelectedDate.Month + " Ano: " + Calendar1.SelectedDate.Year;
            SqlCeCommand updateCommand = new SqlCeCommand(updt, conn);
            updateCommand.Parameters.AddWithValue("@conteudo", conteudo);
            updateCommand.Parameters.AddWithValue("@dia", Calendar1.SelectedDate.Day);
            updateCommand.Parameters.AddWithValue("@mes", Calendar1.SelectedDate.Month);
            updateCommand.Parameters.AddWithValue("@ano", Calendar1.SelectedDate.Year);
            updateCommand.Parameters.AddWithValue("@crianca", CriancaId);
            try
            {
                conn.Open();
                updateCommand.ExecuteNonQuery().ToString();
                this.LabelOK.Text = "Edit com sucesso!";
            }
            catch (Exception exc)
            {
                LabelErro.Text = exc.Message + "-" + exc.Source;
                this.LabelOK.Text = "";
            }
            finally
            {
                conn.Close();
            }
            //Response.Redirect("SeccaoRelatorio.aspx");

        }
    }
}